﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;

public partial class Account_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            previousPage.Value = "";
            if (Session[HelpConstEnum.ADMIN_PRE_URL_SESSION] != null)
            {
                Uri preUrl = (Uri)Session[HelpConstEnum.ADMIN_PRE_URL_SESSION];
                previousPage.Value = preUrl.ToString();
                if (previousPage.Value.Contains("Admin/Login.aspx"))
                    previousPage.Value = "";
            }
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string sql = @"SELECT u.USERS_ID,u.USERS_NAME,u.USERS_PASS,u.FULL_NAME,u.USER_GROUP_ID
FROM USERS u WHERE u.USERS_NAME='" + txtUserName.Text + "'";
        bool ok = false;
        DataTable dt = SQLConnectWeb.GetTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            ok = txtUserName.Text == "admin" && dt.Rows[0][USERS.cl_USERS_PASS].ToString().Trim().Equals(HelpUtil.GetMD5(txtPassword.Text));
        }
        if (ok)
        {
            UserLogin.SaveUserLogin(this, dt.Rows[0][USERS.cl_USERS_NAME].ToString(),
                                                      dt.Rows[0][USERS.cl_USERS_ID].ToString(),
                                                      dt.Rows[0][USERS.cl_FULL_NAME].ToString(),
                                                     dt.Rows[0][USERS.cl_USER_GROUP_ID].ToString());

            Session[HelpConstEnum.ADMIN_IS_LOGIN_SESSION] = "Y";
            if (previousPage.Value != "")
            {
                Response.Redirect(previousPage.Value);
                Session[HelpConstEnum.ADMIN_PRE_URL_SESSION] = null;
            }
            else
                Response.Redirect(HelpConstEnum.ADMIN_DEFAULT_PAGE);
        }
        else
        {
            InvalidLoginValidate.IsValid = false;
            txtUserName.Focus();
        }
    }
}
